﻿using Newtonsoft.Json;
using System.Collections.Generic;

public class BaiduOCRResult
{
    // 通用字段
    [JsonProperty("error_code")]
    public string Error_Code { get; set; }

    [JsonProperty("error_msg")]
    public string Error_Msg { get; set; }

    // 结构化字段
    [JsonProperty("words_result")]
    public Dictionary<string, WordResult> WordsResult { get; set; }

    [JsonProperty("words_result_num")]
    public int WordsResultNum { get; set; }

    [JsonProperty("idcard_number_type")]
    public int IdCardNumberType { get; set; }

    [JsonProperty("image_status")]
    public string ImageStatus { get; set; }

    [JsonProperty("log_id")]
    public long LogId { get; set; }

    // 映射到您原有字段的扩展属性
    public string Name => GetWordValue("姓名");
    public string Ethnic => GetWordValue("民族");
    public string IdNumber => GetWordValue("公民身份号码");
    public string Address => GetWordValue("住址");
    public string Gender => GetWordValue("性别");
    public string BirthDate => GetWordValue("出生");

    // 身份证背面字段（需调用反面识别时填充）
    public string Authority => GetWordValue("签发机关");
    public string ValidDateStart => GetWordValue("签发日期");
    public string ValidDateEnd => GetWordValue("失效日期");

    private string GetWordValue(string key)
    {
        return WordsResult != null && WordsResult.TryGetValue(key, out var value) ? value.Words : null;
    }
}

public class WordResult
{
    [JsonProperty("words")]
    public string Words { get; set; }

    [JsonProperty("location")]
    public LocationInfo Location { get; set; }
}

public class LocationInfo
{
    [JsonProperty("top")]
    public int Top { get; set; }

    [JsonProperty("left")]
    public int Left { get; set; }

    [JsonProperty("width")]
    public int Width { get; set; }

    [JsonProperty("height")]
    public int Height { get; set; }
}